<template>
  <div>
    <!-- 表单 -->
    <el-form :label-position="labelPosition" size="small" :model="form" :inline="true">
      <el-form-item label="校 区" style="font-weight:bold;">
        <el-select v-model="form.campusId" placeholder="请选择校区" style="width:150px;" clearable>
          <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼 宇" style="font-weight:bold;">
        <el-select v-model="form.buildId" placeholder="请选择楼宇" style="width:150px;" clearable>
          <el-option v-for="item in buildName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房间号" style="font-weight:bold;">
        <el-input v-model="form.roomNum" placeholder="请输入房间号" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="房间类型" style="font-weight:bold;">
        <el-select v-model="form.dormType" placeholder="请选择房间类型" style="width:150px;" clearable>
          <el-option label="学生宿舍" value="0"></el-option>
          <el-option label="博士宿舍" value="1"></el-option>
          <el-option label="公用房" value="2"></el-option>
          <el-option label="爱心房" value="3"></el-option>
          <el-option label="工具房" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍状态" style="font-weight:bold;">
        <el-select v-model="form.state" placeholder="请选择宿舍状态" style="width:150px;" clearable>
          <el-option label="空房" value="0"></el-option>
          <el-option label="未满" value="1"></el-option>
          <el-option label="满房" value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 表单 -->

    <!-- 按钮 -->
    <div class="buttonList">
      <div class="buttonList_left">
        <el-button size="small" type="success" icon="el-icon-download" @click="downloadRoom" plain>下载宿舍信息模板</el-button>
        <el-upload style="margin-left:10px;" action="#" :before-upload="beforeUpload" ref="newupload"
          :show-file-list="false" :http-request="uploadHttpRequest" :limit="3" :file-list="fileList">
          <el-button size="small" type="primary" plain icon="el-icon-upload2">导入宿舍信息</el-button>
        </el-upload>
        <el-button size="small" style="margin-left:10px;" type="success" plain icon="el-icon-download"
          @click="exportRoom">导出宿舍信息</el-button>
      </div>
      <div>
        <el-button size="small" class="search_button" type="primary" icon="el-icon-search"
          @click="getDataList">查询</el-button>
        <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
    </div>
    <!-- 按钮 -->

    <el-divider></el-divider>

    <!-- 表格 -->
    <el-table size="medium" ref="multipleTable" :data="tableData" border style="width: 100%"
      @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <el-table-column prop="campusName" label="校区" align="center"></el-table-column>
      <el-table-column prop="buildName" label="楼宇" align="center"></el-table-column>
      <el-table-column prop="roomNum" label="房间号" align="center"></el-table-column>
      <el-table-column prop="floor" label="所在楼层" align="center"></el-table-column>
      <el-table-column prop="peopleNum" label="可住人数" align="center"></el-table-column>
      <el-table-column prop="liveNum" label="已住人数" align="center"></el-table-column>
      <el-table-column prop="dormType" label="房间类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.dormType == 0">学生宿舍</span>
          <span v-if="scope.row.dormType == 1">博士宿舍</span>
          <span v-if="scope.row.dormType == 2">公用房</span>
          <span v-if="scope.row.dormType == 3">爱心房</span>
          <span v-if="scope.row.dormType == 4">工具房</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="男女生宿舍" align="center" width="100">
        <template slot-scope="scope">
          <span v-if="(scope.row.dormType == 0 || scope.row.dormType == 1) && scope.row.type == 0">女生宿舍</span>
          <span v-if="(scope.row.dormType == 0 || scope.row.dormType == 1) && scope.row.type == 1">男生宿舍</span>
        </template>
      </el-table-column>
      <el-table-column prop="dormInfo" label="备注信息" align="center"></el-table-column>
      <el-table-column prop="state" label="宿舍状态" align="center">
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.state == 0">空房</span>
          <span v-if="scope.row.state == 1">未满</span>
          <span v-if="scope.row.state == 2">满房</span> -->
          <el-button size="mini" type="warning" plain v-if="(scope.row.state == 0)" disabled>空房</el-button>
          <el-button size="mini" type="primary" plain v-if="(scope.row.state == 1)" disabled>未满</el-button>
          <el-button size="mini" type="success" plain v-if="(scope.row.state == 2)" disabled>满房</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="handle" label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="updateRoom(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" plain @click="deleteRoom({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->

    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>


    <!-- 修改宿舍信息弹窗 -->
    <el-dialog title="修改宿舍信息" :visible.sync="dialog_change">
      <el-form size="small" :model="formChange" :label-position="labelPosition" label-width="100px"
        style="margin-left:20%;">
        <el-form-item label="楼宇 :" style="font-weight:bold;">
          <el-select v-model="formChange.buildId" placeholder="请选择楼宇" style="width:60%">
            <el-option v-for="item in buildName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间号 :" style="font-weight:bold;">
          <el-input v-model="formChange.roomNum" style="width:60%" placeholder="请输入房间号"></el-input>
        </el-form-item>
        <!-- <el-form-item label="住宿费用 :" >
          <el-input v-model="formChange.expenses" style="width:60%" placeholder="请输入住宿费用" ></el-input>
        </el-form-item> -->
        <el-form-item label="所在楼层 :" style="font-weight:bold;">
          <el-input v-model="formChange.floor" style="width:60%" placeholder="请输入所在楼层"></el-input>
        </el-form-item>
        <el-form-item label="可住人数 :" style="font-weight:bold;">
          <el-input v-model="formChange.peopleNum" style="width:60%" placeholder="请输入可住人数"></el-input>
        </el-form-item>
        <el-form-item label="已住人数 :" style="font-weight:bold;">
          <el-input v-model="formChange.liveNum" style="width:60%" placeholder="请输入已住人数"></el-input>
        </el-form-item>
        <el-form-item label="宿舍状态 :" style="font-weight:bold;">
          <el-select v-model="formChange.state" placeholder="请选择" style="width:60%">
            <el-option label="空房" value="0"></el-option>
            <el-option label="未满" value="1"></el-option>
            <el-option label="满房" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="男女生宿舍 :" style="font-weight:bold;">
          <el-select v-model="formChange.type" placeholder="请选择" style="width:60%" clearable>
            <el-option label="女生宿舍" value="0"></el-option>
            <el-option label="男生宿舍" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间类型 :" style="font-weight:bold;">
          <el-select v-model="formChange.dormType" placeholder="请选择房间类型" style="width:60%">
            <el-option label="学生宿舍" value="0"></el-option>
            <el-option label="博士宿舍" value="1"></el-option>
            <el-option label="公用房" value="2"></el-option>
            <el-option label="爱心房" value="3"></el-option>
            <el-option label="工具房" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息 :" style="font-weight:bold;">
          <el-input v-model="formChange.dormInfo" type="textarea" style="width:60%" placeholder="请输入备注信息"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialog_change = false">取 消</el-button>
        <el-button size="small" type="primary" @click="updateRoomConfirm" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改宿舍信息弹窗 -->
  </div>
</template>

<script>
import { getRoomList, updateRoom, deleteRoom, downloadRoom, exportRoom, uploadRoom } from '_api/DormManage/Room/index.js'
import { getCampusName, getBuildingName } from '_api/DormManage/Building/index.js'
export default {
  data() {
    return {
      labelPosition: "right",
      dialog_change: false,
      form: {
        buildId: "",
        campusId: "",
        current: 0,
        dormType: "",
        field: "",
        order: "",
        pageSize: 10,
        roomNum: "",
        state: ""
      },
      formChange: {
        buildId: "",
        dormInfo: "",
        dormType: "",
        type: "",
        // expenses: "",
        floor: "",
        id: "",
        peopleNum: "",
        liveNum: "",
        roomNum: "",
        state: ""
      },
      tableData: [],
      CampusName: [],
      buildName: [],
      fileList: [],
      timeNow: "",
      total: 0
    }
  },
  watch: {
    form: {
      // 执行方法
      handler(oldValue, newValue) {
        getBuildingName({ campusId: newValue.campusId, type: 0 }).then(res => {
          if (res.status == 200) {
            this.buildName = res.data.data
          }
        })
      },
      deep: true, // 深度监听
      // immediate: true  // 第一次改变就执行
    }
  },
  methods: {
    handleSelectionChange() {

    },
    //重置
    reset() {
      this.form = {
        buildId: "",
        campusId: "",
        current: 0,
        dormType: "",
        field: "",
        order: "",
        pageSize: 10,
        roomNum: "",
        state: ""
      }
      this.getRoomList()
    },
    // 导入信息
    uploadHttpRequest(data) {
      console.log(data)
      let fd = new FormData()
      fd.append('file', data.file)
      uploadRoom(fd).then(res => {
        if (res.data.success == true) {
          this.$message.success("导入成功！")
          this.reset()
        } else {
          this.$message({
            message: res.data.message,
            duration: 3000
          })
        }
      })
    },
    beforeUpload(file) {
      // console.log(file)
      // 校验
      let legalName = ["xlsx", "xls"];
      // 拿到后缀名
      let name = file.name.substring(
        file.name.lastIndexOf(".") + 1,
        file.name.length
      );
      if (legalName.includes(name)) {
        // console.log(legalName.includes(name));
      } else {
        this.$message.info("文件格式不对,仅限xls和xlsx");
        return false;
      }
    },
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
      console.log(_this.gettime)
      this.timeNow = _this.gettime
    },
    // 下载宿舍信息模板
    downloadRoom() {
      downloadRoom().then(res => {
        // console.log(res, "121212121212")
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '宿舍信息模板_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('下载成功！')
      })
    },
    // 导出宿舍信息
    exportRoom() {
      exportRoom(this.form).then(res => {
        // console.log(res.data,"121212121")
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '宿舍信息报表_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    resetFormChange() {
      this.formChange = {
        buildId: "",
        dormInfo: "",
        dormType: "",
        type: "",
        floor: "",
        id: "",
        peopleNum: "",
        liveNum: "",
        roomNum: "",
        state: ""
      }
    },
    //修改宿舍信息弹窗
    updateRoom(e) {
      console.log(e)
      this.resetFormChange()
      this.dialog_change = true
      this.buildName.forEach(item => {
        if (item.name == e.buildName) {
          this.formChange.buildId = item.id
        }
      })
      this.formChange.dormInfo = e.dormInfo
      this.formChange.dormType = e.dormType
      // if (e.type) {
      //   this.formChange.type = e.type.toString()
      // }
      // this.formChange.expenses = e.expenses
      this.formChange.floor = e.floor
      this.formChange.id = e.id
      this.formChange.peopleNum = e.peopleNum
      this.formChange.liveNum = e.liveNum
      this.formChange.state = e.state
      this.formChange.roomNum = e.roomNum
      this.formChange.type = e.type.toString()
      console.log(this.formChange)
    },
    //修改宿舍信息确认
    updateRoomConfirm() {
      updateRoom(this.formChange).then(res => {
        console.log(res)
        if (res.data.success == true) {
          this.dialog_change = false
          this.$message({
            message: '修改成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.getRoomList()
            }
          })
        } else {
          this.$message({
            message: res.data.message,
            duration: 1000
          })
        }
      })
    },

    //删除宿舍
    deleteRoom(e) {
      this.$confirm('此操作将删除该房间, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRoom(e).then(res => {
          if (res.status == 200) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.getRoomList()
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCurrentChange(e) {
      //当前页
      this.form.current = (e - 1) * 10
      // this.form.current = e
      this.getRoomList()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getRoomList()
    },
    // 查询按钮
    getDataList() {
      this.form.current = 0
      this.getRoomList()
    },
    //  查询房间列表
    getRoomList() {
      getRoomList(this.form).then(res => {
        // console.log(res)
        if (res.data.success == true) {
          this.getCampusName()
          this.getBuildingName()
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      }).catch(error => {
        // console.log(error.response.status);
        // if (error.response.status == 401) {
        //   this.$confirm('登录状态过期, 请重新登录!', '提示', {
        //     confirmButtonText: '确定',
        //     // cancelButtonText: '取消',
        //     showCancelButton: false,
        //     type: 'warning'
        //   }).then(() => {
        //     this.$router.push('/login')
        //   }).catch(() => {

        //   })
        // }
      })
    },
    //获取校区名称
    getCampusName() {
      getCampusName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.CampusName = res.data.data
        }
      })
    },
    //获取楼宇名称
    getBuildingName() {
      getBuildingName({ type: 0 }).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.buildName = res.data.data
        }
      })
    }
  },
  created() {
    this.getRoomList()
    // this.getCampusName()
    // this.getBuildingName()
  },
};
</script>
<style lang="scss" scoped>
.search_button {
  margin-left: 80px;
}

.reset_button {
  margin-left: 10px;
}

.dialogbutton {
  margin-right: 40%;
}

.buttonList {
  display: flex;
  width: 80%;
  height: 34px;
  justify-content: space-between;
}

.buttonList_left {
  margin-left: 44px;
  display: flex;
  width: 400px;
  justify-content: space-between;
}

.pagination {
  float: right;
  margin-top: 10px;
}
</style>
